<template>
	<view>
		<!-- <view class="header_left" @tap="hui" ><image  src="/static/left2.png"  class="left_img"/></view>
		<view class="header">订单详情</view> -->
	
		<view v-if="addrlength.length > 0">
		<view class="address" @tap="selectAddress">
			<view class="address_top">{{addr.addrs}}{{addr.addrmore}}</view>
			<view class="address_content">{{addr.name}}  {{addr.phone}}</view>
		</view>
		</view><view v-else>
		<!-- 收货地址 -->
		<view class="addr" @tap="selectAddress">
			<text class="dizhi">选择地址</text>
			<image class="tianjia" src="../../static/jiahao1.png"></image>
		</view>
		</view>
		<!-- 购买商品列表 -->
		<view class="buy-list">
			<view class="row" >
				<view class="goods-info">
					<view class="img">
						<image :src="wz+buylist.pimg"></image>
					</view>
					<view class="info">
						<view class="title">{{buylist.pname}}</view>
						<view class="spec"> 数量:{{buylist.num}}</view>
						<view class="price-number">
							<view class="price">￥{{buylist.price}}</view>
							<view class="number">

							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 提示-备注 -->
		<view class="order">
			<!-- <view class="row">
				<view class="left">
					积分 :
				</view>
				<view class="right">
					已扣除{{int}}积分抵扣{{deduction|toFixed}}元
				</view>
			</view> -->
			<view class="row">
				<view class="left">
					备注 :
				</view>
				<view class="right">
					<input  v-model="note" />
				</view>
			</view>
		</view>
		<!-- 明细 -->
		<view class="detail">
			<view class="row">
				<view class="nominal">
					商品金额
				</view>
				<view class="content">
					￥{{buylist.price*buylist.num|toFixed}}
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					运费
				</view>
				<view class="content">
					￥{{buylist.freight|toFixed}}
				</view>
			</view>
		</view>
		<view class="blck">

		</view>
		<view class="btncss">
			<radio-group @change="btn">
			<radio :checked="true" value="5">购物币</radio>
			</radio-group>
		</view>
		<!-- 验证码 -->
			<view class="cash_in_yzm"><view class="yzm_text">验证码</view><input class="yzm" type="text" name="yzm" @input="codenum" placeholder="手机验证码" />
			<view v-if="show_yzm">
				<view class="yzm_btn" @tap="yzm">获取验证码</view>
			</view>
			<view v-else>
				<view class="yzm_btn2">剩余{{seconde}}s</view>
			</view>
			</view>
			<view style="width: 100%;text-align: center;color:#808080 ;">购物币支付请获取验证码验证。</view>
		<!-- 验证码结束	 -->
		<view class="footer">
			<view class="settlement">
				<view class="sum">合计:<view class="money">￥{{sum|toFixed}}</view>
				</view>
				<view class="btn" @tap="toPay">提交订单</view>
			</view>
		</view>
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id:'',
				addr:'',
				addrlength:'',
				sum:'',
				addid:'0',
				show_yzm:true,
				seconde:'60',
				yh_data:'',
				code_num:'',
				wz:'http://tuiguang.yiyunoto.com',
				buylist: [], //订单列表
				goodsPrice: 0.0, //商品合计价格
				sumPrice: 0.0, //用户付款价格
				freight: 0.00, //运费
				note: '', //备注
				

			};
		},
		onShow() {
			//页面显示时，加载订单信息
			uni.getStorage({
				key: 'buylist',
				success: (ret) => {
					this.buylist = ret.data;
					this.goodsPrice = 0;
					//合计
					let len = this.buylist.length;
					for (let i = 0; i < len; i++) {
						this.goodsPrice = this.goodsPrice + (this.buylist[i].num * this.buylist[i].price);
					}
					this.deduction = this.int / 100;
					this.sumPrice = this.goodsPrice - this.deduction + this.freight;
				}
			});
		},
		onLoad(options) {
		
			console.log("获取id",options.id)
			console.log("获取id",options.num)
			this.id=options.id
			this.num=options.num
			const id=options.id
			// 用户信息请求
			uni.request({
				url:'http://tuiguang.yiyunoto.com/api/member/tixianmoney',
				header:{authtoken:this.$store.state.token},
				success:(res)=>{
					console.log("11请求成功",res)
					this.yh_data=res.data.data.phone
				},
				fail:(res)=>{
					console.log("请求失败",res)
				}
			})
			// 订单数据请求
			uni.request({
				url:"http://tuiguang.yiyunoto.com/api/orders/readdorder",
				method:'POST',
				header:{authtoken:this.$store.state.token},
				data:{id:this.id,
					num:this.num,
				},  
				success:(res)=>{
					console.log("请求成功",res)
					console.log("123请求成功",res.data.data.product)
						uni.showToast({
						title:res.data.msg
					})
					this.buylist=res.data.data.product
					this.sum=parseFloat(this.buylist.num*this.buylist.price)+parseFloat(this.buylist.freight)
				},
				fail:(res)=>{
					console.log("请求失败",res)
				}
			})
			// 地址请求
			uni.request({
				url:"http://tuiguang.yiyunoto.com/api/member/mem_addr",
				method:'POST',
				header:{authtoken:this.$store.state.token},  
				success:(res)=>{
					console.log("请求成功",res)
					this.addr=res.data.data[this.addid]
					this.addrlength=res.data.data
				},
				fail:(res)=>{
					console.log("请求失败",res)
				}
			})
		},
		onBackPress() {
			//页面后退时候，清除订单信息
			this.clearOrder();
		},
		filters: {
			toFixed: function(x) {
				return parseFloat(x).toFixed(2);
			}
		},
		methods: {
			onPullDownRefresh: function() {
				uni.request({
					url:"http://tuiguang.yiyunoto.com/api/member/mem_addr",
					method:'POST',
					header:{authtoken:this.$store.state.token},  
					success:(res)=>{
						console.log("请求成功",res)
						this.addr=res.data.data[this.addid]
						this.addrlength=res.data.data
					},
					fail:(res)=>{
						console.log("请求失败",res)
					}
				})
			},
			codenum(e){
				console.log(e.detail.value)
				this.code_num=e.detail.value
			},
			yzm:function(){
				console.log("获取验证码",this.tel)
					uni.request({
							url: 'http://tuiguang.yiyunoto.com/api/login/smsSend',
							method:'POST',
							header:{authtoken:this.$store.state.token},
							data:{
								phone:this.yh_data,
							},
								success: (res) => {
									this.$api.msg(res.data.msg);
									if(res.data.msg=="短信验证码发送成功"){
									this.show_yzm=false,
									setInterval(()=>{
										--this.seconde
									},1000)
									setTimeout(()=>{
										this.show_yzm=true,
										this.seconde=60},
										60000
									)}
							console.log("ya",res.data.msg)							// console.log(res.data.data);
							}
						});
				},
			hui:function(){
				this.clearOrder();
				uni.switchTab({
				            url:'../shop_car/index',  
				        })

			},
			clearOrder() {
				uni.removeStorage({
					key: 'buylist',
					success: (res) => {
						this.buylist = [];
						console.log('remove buylist success');
					}
				});
			},
			toPay() {
				//商品列表
				let paymentOrder = [];
				let goodsid = [];
				let len = this.buylist.length;
				for (let i = 0; i < len; i++) {
					paymentOrder.push(this.buylist[i]);
					goodsid.push(this.buylist[i].id);
				}
				if (this.addrlength.length==0) {
					this.$api.msg('订单地址信息未填写请添加。');
					return;
				}else{
					
					console.log("返回请求成功12",this.buylist.id,)
					console.log("返回请求成功123",this.addr.id,)
					uni.request({
						
						url:"http://tuiguang.yiyunoto.com/api/orders/reorder",
						data:{
							paytype:'5',
							oid:this.buylist.oid,
							addrid:this.addr.id,
							phone:this.yh_data,
							code:this.code_num,
						},
						header:{authtoken:this.$store.state.token},
						method:'POST',
						success:(res)=>{
							console.log("返回请求成功",res)
							this.$api.msg(res.data.msg)
							if(res.data.code==1){
								 uni.navigateTo({
									url:'../Pay_success/Pay_success?txt='+res.data.msg
								 })
							}
							// if(res.data.msg=="签名成功"){
							// 	console.log("返回请求成功",res.data.data.noncestr)
								// uni.getProvider({
								// 	service:"payment",
								// 	success: (res) => {
								// 		console.log(res);
								// 	}
								// }),
								// uni.requestPayment({
								//           provider: 'wxpay',
								//           orderInfo: {
								//            "appid": 'wx305aceb0e7d03265',
								//            "noncestr": res.data.data.noncestr,
								//            "package": "Sign=WXPay",
								//            "partnerid": res.data.data.partnerid,
								//            "prepayid": res.data.data.prepayid,
								//            "timestamp": res.data.data.timestamp,
								//            "sign": res.data.data.sign,
								//           },
								//          success: function (res) {
								//                 console.log('success:' + JSON.stringify(res));
								//             },
								//             fail: function (err) {
								//                 console.log('fail:' + JSON.stringify(err));
								//             }
								//         });
							// }
						},
						fail:(res)=>{
							console.log("请求失败",res)
						}
					})
				}
				//本地模拟订单提交UI效果
				uni.showLoading({
					title: '正在提交订单...'
				})
				setTimeout(() => {
					uni.setStorage({
						key: 'paymentOrder',
						data: paymentOrder,
						success: () => {
							uni.hideLoading();
							uni.redirectTo({
								url: "../pay/payment/payment?amount=" + this.sumPrice
							})
						}
					})
				}, 1000)

			},
			//选择收货地址
			selectAddress() {
				uni.navigateTo({
					url: '../address/address?id='+this.id
				})
			}
		}
	}
</script>

<style lang="scss">
	.header {
		text-align: center;
	}
	.header_left{
		width: 100upx;
		height: 60upx;
		font-size: 28upx;
		font-family: PingFangSC-Regular, PingFangSC;
		color: rgba(102, 102, 102, 1);
		padding-bottom: 10upx;
		
	}
	.left_img{
		width: 20upx;
		height: 40upx;
		padding-top: 50upx;
		padding-left: 30upx;
	}
	.header {
		text-align: center;
		font-size: 35upx;
		padding-bottom: 20upx;
		font-family: PingFangSC-Medium, PingFangSC;
		color: rgba(87, 87, 87, 1);
		
	}
	.address{
		width:86% ;
		padding: 20upx 3%;
		margin: 0 auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
	}
	.address_top{
		width: 100%;
	}
	.address_content{
		padding-top: 10upx;
		color:#606266;
		box-sizing: border-box;
	}

	.addr {
		width: 86%;
		padding: 20upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		display: flex;

		.icon {
			width: 80upx;
			height: 80upx;
			display: flex;
			align-items: center;

			image {
				width: 60upx;
				height: 60upx;
			}

		}

		.dizhi {

			margin-top: 15upx;
			margin-bottom: 10upx;
			margin-left: 10upx;
			font-size: 35upx;
		}

		.tianjia {
			margin-top: 20upx;
			margin-bottom: 10upx;
			margin-left: 5upx;
			width: 37upx;
			height: 37upx;
		}
	}

	.qq {}

	.buy-list {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			margin: 30upx 0;

			.goods-info {
				width: 100%;
				display: flex;

				.img {
					width: 22vw;
					height: 22vw;
					border-radius: 10upx;
					overflow: hidden;
					flex-shrink: 0;
					margin-right: 10upx;

					image {
						width: 22vw;
						height: 22vw;
					}
				}

				.info {
					width: 100%;
					height: 22vw;
					overflow: hidden;
					display: flex;
					flex-wrap: wrap;
					position: relative;

					.title {
						width: 100%;
						font-size: 28upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						// text-align: justify;
						overflow: hidden;
					}

					.spec {
						font-size: 22upx;
						background-color: #f3f3f3;
						color: #a7a7a7;
						height: 40upx;
						display: flex;
						align-items: center;
						padding: 0 10upx;
						border-radius: 20upx;
						margin-bottom: 20vw;
					}

					.price-number {
						position: absolute;
						width: 100%;
						bottom: 0upx;
						display: flex;
						justify-content: space-between;
						align-items: flex-end;
						font-size: 28upx;
						height: 40upx;

						.price {
							color: red;
						}

						.number {
							display: flex;
							justify-content: center;
							align-items: center;

						}
					}
				}
			}
		}
	}

	.order {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			margin: 20upx 0;
			height: 40upx;
			display: flex;

			.left {
				font-size: 30upx;
			}
		}
	}

	.blck {
		width: 100%;
		height: 100upx;
	}
// 验证码
.cash_in_yzm{
		width: 100%;
		height: 100upx;
		padding: 30upx 30upx;
		margin-top: 18upx;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		box-sizing: border-box;
		background: #FFFFFF;
	}
	.yzm_text{
		width:25%;
		float: left;
		
	}
	.yzm {
		width:50%;
		float: left;
	}

	.yzm_btn {
		width: 25%;
		font-size: 26upx;
		text-align: right;
		float: right;
		color: blue;

	}
	.yzm_btn2{
		width:25%;
		font-size: 26upx;
		text-align: right;
		float: right;
		color: #DBDADA;
	}
	.footer {
		width: 92%;
		padding: 0 4%;
		background-color: #fbfbfb;
		height: 100upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 28upx;
		position: fixed;
		bottom: 0upx;
		z-index: 5;

		.settlement {
			width: 80%;
			display: flex;
			justify-content: flex-end;
			align-items: center;

			.sum {
				width: 50%;
				font-size: 28upx;
				margin-right: 10upx;
				display: flex;
				justify-content: flex-end;

				.money {
					font-weight: 600;
				}
			}

			.btn {
				padding: 0 30upx;
				height: 60upx;
				background-color: #FF6C00;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30upx;
				border-radius: 40upx;
			}
		}
	}

	.detail {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			height: 60upx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.nominal {
				font-size: 28upx;
			}

			.content {
				font-size: 26upx;
				color: #FF6C00;
			}
		}
	}
</style>
